package com.conf.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author qiujunda
 * @date 2019年2月14日
 * @description
 */
public class JedisPoolUtil {

    private JedisPool pool = null;

    private int databaseIndex = 0;

    public JedisPoolUtil(String hostName, int port, int databaseIndex,
                         int maxTotal, int maxIdle, int minIdle) {
        System.out.println("--------------------------------" + hostName);
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(50);//最大连接数，连接全部用完，进行等待
        poolConfig.setMinIdle(5); //最小空余数
        poolConfig.setMaxIdle(10); //最大空余数
        this.databaseIndex = databaseIndex;
        pool = new JedisPool(poolConfig, hostName, port);
//		pool = new JedisPool(hostName,port);
    }

    public Jedis getJedis() {
        Jedis jedis = pool.getResource();
        System.out.println(jedis.hashCode());
        jedis.select(databaseIndex);
        return jedis;
    }

    public void reset(Jedis jedis) {
        pool.returnResource(jedis);
    }
}
